# coding:utf-8
from _prototype import plugin_prototype
import sys
from cross_platform import *
# start meta
__plugin_name__ = 'reverse compound assistant'
__author = 'fffonion'
__version__ = 0.1
hooks = {}
extra_cmd = {'rq':'reverse_compound', 'reverse_compound':'reverse_compound'}
# end meta

# query item count
lvdata = [(1, 0, 300, 100), (2, 100, 490, 160), (3, 260, 810, 220), (4, 480, 1230, 280), (5, 760, 1760, 340), (6, 1100, 2390, 400), (7, 1500, 3110, 460), (8, 1960, 3920, 520), (9, 2480, 4810, 580), (10, 3060, 5780, 640), (11, 3700, 6820, 700), (12, 4400, 7930, 760), (13, 5160, 9100, 820), (14, 5980, 10330, 880), (15, 6860, 11610, 940), (16, 7800, 12940, 1000), (17, 8800, 14310, 1060), (18, 9860, 15710, 1120), (19, 10980, 17140, 1180), (20, 12160, 18600, 1240), (21, 13400, 20070, 1300), (22, 14700, 21560, 1360), (23, 16060, 23060, 1420), (24, 17480, 24570, 1480), (25, 18960, 26070, 1540), (26, 20500, 27570, 1600), (27, 22100, 29050, 1660), (28, 23760, 30510, 1720), (29, 25480, 31960, 1780), (30, 27260, 33370, 1840), (31, 29100, 34750, 1900), (32, 31000, 36100, 1960), (33, 32960, 37390, 2020), (34, 34980, 38640, 2080), (35, 37060, 39840, 2140), (36, 39200, 40970, 2200), (37, 41400, 42040, 2260), (38, 43660, 43040, 2320), (39, 45980, 43970, 2380), (40, 48360, 44810, 2440), (41, 50800, 45570, 2500), (42, 53300, 46230, 2560), (43, 55860, 46800, 2620), (44, 58480, 47270, 2680), (45, 61160, 48000, 2740), (46, 63900, 48000, 2800), (47, 66700, 48000, 2860), (48, 69560, 48000, 2920), (49, 72480, 48000, 2980), (50, 75460, 48000, 3040), (51, 78500, 48000, 3100), (52, 81600, 48000, 3160), (53, 84760, 48000, 3220), (54, 87980, 48000, 3280), (55, 91260, 48000, 3340), (56, 94600, 48000, 3400), (57, 98000, 48000, 3460), (58, 101460, 48000, 3520), (59, 104980, 48000, 3580), (60, 108560, 48000, 3600), (61, 112160, 48000, 3600), (62, 115760, 48000, 3600), (63, 119360, 48000, 3600), (64, 122960, 48000, 4000), (65, 126960, 48000, 4000), (66, 130960, 48000, 4000), (67, 134960, 48000, 4000), (68, 138960, 48000, 4000), (69, 142960, 48000, 4500), (70, 147460, 48000, 4500), (71, 151960, 48000, 4500), (72, 156460, 48000, 4500), (73, 160960, 48000, 4500), (74, 165460, 48000, 5000), (75, 170460, 48000, 5000), (76, 175460, 48000, 5000), (77, 180460, 48000, 5000), (78, 185460, 48000, 5000), (79, 190460, 48000, 5500), (80, 195960, 48000, 5500), (81, 201460, 48000, 5500), (82, 206960, 48000, 5500), (83, 212460, 48000, 5500), (84, 217960, 48000, 6000), (85, 223960, 48000, 6000), (86, 229960, 48000, 6000), (87, 235960, 48000, 6000), (88, 241960, 48000, 6000), (89, 247960, 48000, 6500), (90, 254460, 48000, 6500), (91, 260960, 48000, 6500), (92, 267460, 48000, 6500), (93, 273960, 48000, 6500), (94, 280460, 48000, 7000), (95, 287460, 48000, 7000), (96, 294460, 48000, 7000), (97, 301460, 48000, 7000), (98, 308460, 48000, 7000), (99, 315460, 48000, 7500), (100, 322960, 48000, 7500), (101, 330460, 48000, 7500), (102, 337960, 48000, 7500), (103, 345460, 48000, 7500), (104, 352960, 48000, 8000), (105, 360960, 48000, 8000), (106, 368960, 48000, 8000), (107, 376960, 48000, 8000), (108, 384960, 48000, 8000), (109, 392960, 48000, 8500), (110, 401460, 48000, 0)]
def reverse_compound(plugin_vals):
    def do(args):
        args = args or 'card.star>4'
        logger = plugin_vals['logger']
        player_card = plugin_vals['player'].card
        compound_candidates = {}
        material_candidates = []
        for card in player_card.cards:
            cid = card.master_card_id
            if cid not in compound_candidates:
                cids = player_card.cid(cid)
                if len(cids) > 1:  # 不止一张
                    compound_candidates[cid] = cids
            if card.lv <= 10 and player_card.db[cid][1] < 5 and cid not in compound_candidates and card.holography != '1':
                material_candidates.append(card)
        print(du8('发现了以下逆合成路线'))
        for comp in compound_candidates:
            compound_candidates[comp] = sorted(compound_candidates[comp], key = lambda x:int(x.holography) * +100 - x.lv)  # 闪卡最后
            print(du8('%4s.[%s]  %s' % (
                comp,
                player_card.db[comp][0],
                '->'.join(list(map(lambda x:str(x.lv) + (x.holography == '1' and '-HOLO' or ''), compound_candidates[comp])))
            )))
        while True:
            idinp = raw_inputd('%s\n输入欲合成的卡片id, e退出> ' % ('-' * 20)) or '-1'
            if idinp == 'e':
                break
            idinp = int(idinp)
            if idinp not in compound_candidates:
                print(du8('所选id不存在于逆合成路线中!'))
                continue
            selcards = compound_candidates[idinp]
            cnt = raw_inputd('当前拥有[%s]%d张，使用多少张卡片用于逆合成？> ' % (player_card.db[idinp][0], len(selcards)))
            if cnt > len(selcards):
                cnt = len(selcards)
            # lv_table a(1,0,300,100) lv,start_exp,comp_gold,exp_lv
            # total_exp=nomarlity_base+(exp_1~exp_lv-1)*0.7
    return do
